# -*- coding: utf-8 -*-
import scrapy
from lianjiaPro.items import LianjiaproItem
import re
import json


class RoomSpider(scrapy.Spider):
    name = 'room'
    # allowed_domains = ['www.xxx.com']
    start_urls = ["https://bj.lianjia.com/ershoufang"]

    url = "https://bj.lianjia.com/ershoufang/pg%d"
    page = 2

    def parse(self, response):
        li_list = response.xpath('//*[@id="leftContent"]/ul//li[@class="clear LOGCLICKDATA"]')

        for li in li_list:
            # 房源标题
            title = li.xpath('./div/div[1]/a/text()').extract_first()

            # 详情信息的url
            detail_url = li.xpath('./div/div[1]/a/@href').extract_first()

            # 房间信息列表
            room_info_list = li.xpath('./div/div[2]/div//text()').extract()

            # 小区名称
            communityname = room_info_list[0]

            # 户型
            roomtype = room_info_list[2]

            # 面积
            area = room_info_list[4]

            # 朝向
            orientation = room_info_list[6]

            # 单价
            unitprice_1 = li.xpath('./div/div[4]/div[2]/div[2]/span/text()').extract_first()
            unitprice = re.search(r'\d+', unitprice_1).group()

            # 总价
            price = li.xpath('./div/div[4]/div[2]/div[1]//text()').extract_first() + '万'

            # 实例化item对象
            item = LianjiaproItem()
            item['title'] = title
            item['communityname'] = communityname
            item['roomtype'] = roomtype
            item['area'] = area
            item['orientation'] = orientation
            item['unitprice'] = unitprice
            item['price'] = price

            yield scrapy.Request(url=detail_url, callback=self.detail_parse, meta={'item': item})

        if self.page <= 100:
            new_url = format(self.url % self.page)
            self.page += 1
            yield scrapy.Request(url=new_url, callback=self.parse)

    # 详情页数据
    def detail_parse(self, response):
        item = response.meta['item']
        # 区域
        areaname = response.xpath('/html/body/div[5]/div[2]/div[6]/div[2]/span[2]//text()').extract_first()
        # 链家编号
        houserecord = response.xpath('/html/body/div[5]/div[2]/div[6]/div[4]/span[2]/text()').extract_first()
        # 坐标的rid
        rid = response.xpath('/html/body/div[3]/div/div/div[2]/@data-lj_action_housedel_id').extract_first()

        place_url = 'https://bj.lianjia.com/ershoufang/housestat?hid=%s&rid=%s' % (houserecord, rid)

        item['areaname'] = areaname
        item['houserecord'] = houserecord
        item['rid'] = rid

        yield scrapy.Request(url=place_url, callback=self.place_parse, meta={'item': item})

    # 坐标数据
    def place_parse(self, response):

        item = response.meta['item']
        place_text = response.text
        place = json.loads(place_text)['data']['resblockPosition']

        # 经度
        longitude = place.split(',')[0]
        # 纬度
        latitude = place.split(',')[1]

        item['longitude'] = longitude
        item['latitude'] = latitude

        yield item
